<?php
class User_Model_Ward extends Zend_Db_Table{
    protected $_name = 'Ward';
    protected $_primary = 'ID';
    public function listWard()
    {
        return $this->fetchAll()->toArray();
    }
	public function listHaveWard()
    {
    	$db = Zend_Registry::get('db');
        $sql='SELECT * FROM Ward where lat<>0 and ID>=300000';
		$have=array();
		$have=$db->fetchAll($sql);
		//var_dump($have);
		//die;
        return $have;
    }
	public function listNoneWard()
    {
    	$db = Zend_Registry::get('db');
        $sql='SELECT * FROM Ward where lat=0 and ID>411010';
		$have=array();
		$have=$db->fetchAll($sql);
        return $have;
    }
	public function NameWard($id)
	{
		$list=$this->listWard();
		foreach ($list as $value) {
			if($value['ID']==$id){	
				return $value['Name'];
			}
		}
	}
	public function isWard($id)
	{
		$list=$this->listWard();
		foreach ($list as $value) {
			if($value['ID']==$id){	
				return true;
			}
		}
		return false;
	}
	
	public function listURLWard()
	{
		$listurl = array();
		$list=$this->listNoneWard();		
		foreach ($list as $value) {
			$id=0+$value['ID'];
			$idhuyen=$id/100;
			$idtinh=$idhuyen/100;
			$province= new User_Model_Province();
			$district=new User_Model_District();	
			//$id=0+$value['ID'];
			//$province= new User_Model_Province();
			//$idtinh=$id/100;
			$address=	$value['Name'].",".( $district->NameDistrict((int)$idhuyen)).",".($province->NameProvince((int)$idtinh)).",VN";
			
			$url='http://maps.google.com/maps/geo?q=' . $address . '&output=xml&sensor=false';			
			//$url= "http://maps.googleapis.com/maps/api/geocode/xml?address=".$value['Name'].",".( $district->NameDistrict((int)$idhuyen)).",".($province->NameProvince((int)$idtinh)).",VN&sensor=true";
			//$url= "http://maps.googleapis.com/maps/api/geocode/xml?address=".$value['Name'].",VN&sensor=true";
			$url=array('ID' => $id,'url' => $url);			
			array_push($listurl,$url);
		}		
		//var_dump($list);
		//	die;
		return $listurl;
	}
	public function getOneWard($id)
    {
        $db = Zend_Registry::get('db');
        $sql='SELECT * FROM Ward where id=?';
        return $db->fetchRow($sql,$id);
    }
    public function updateWard($id, $lat, $lng){
        $db = Zend_Registry::get('db');
        $where = ' ID = '.$id;
        $data = array(                    
            'lat'     => $lat,
            'lng'  => $lng
        );    
        $db->update('Ward', $data, $where);
    }
    public function addWard($id, $name, $lat, $lng){
        $db = Zend_Registry::get('db');
        $data = array( 
            'ID'    =>$id,                   
            'name'     => $name,
            'lat'  => $lat,
            'lng'  => $lng
        );    
        $db->insert('Ward',$data);
    }
	
}
?>